import { getInitOptions } from '@/config/init'
import router from '@/router'

export function expr (url: string) {
  const u = new URL(getInitOptions()?.api)
  const protocol = u.protocol
  const hostname = u.hostname
  const port = u.port
  const pathname = u.pathname
  const body = 'return `' + url.replace(/({\w+})/g, (_, c) => `$${_}`) + '`'
  // eslint-disable-next-line
  return new Function('protocol', 'hostname', 'port', 'pathname', body)(protocol, hostname, port, pathname)
}

export function queryString (query: Record<string, any>) {
  return Object.entries(query).filter(([k, v]) => v !== undefined).map(([k, v]) => `${k}=${v}`).join('&')
}

export function openWindow (path: string) {
  if (process.env.NODE_ENV === 'development') {
    window.open(router.resolve(path).href)
  } else {
    window.open('app://./index.html#' + path)
  }
}
